//==========================================================================//
//                                                                          //
//  RTCDS1307 Library              Version 0.1                     March 2012  //
//                                                                          //
//  Written for the Arduino Duemilanove w/ ATmega328                        //
//                                                                          //
//  RTCDS1307 Arduino Library                                                  //
//                                                                          //
//  Created by JFLagarde (jeanfrancoislagarde@hotmail.com)                  //
//                                                                          //
//  This file is part of OpenWS (http://code.google.com/p/openws/)          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Notes:                                                                  //
//                                                                          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Copyright 2012 Jean-Francois Lagarde (jeanfrancoislagarde@hotmail.com)  //
//  This program is distributed under the terms of                          //
//  the GNU General Public License                                          //
//                                                                          //
//  This program is free software: you can redistribute it and/or modify    //
//  it under the terms of the GNU General Public License as published by    //
//  the Free Software Foundation, either version 3 of the License, or       //
//  (at your option) any later version.                                     //
//                                                                          //
//  This program is distributed in the hope that it will be useful,         //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//  GNU General Public License for more details.                            //
//                                                                          //
//  You should have received a copy of the GNU General Public License       //
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                          //
//==========================================================================//

#ifndef RTCDS1307_h
	#define RTCDS1307_h

	#if defined(ARDUINO) && ARDUINO >= 100
		#include "Arduino.h"
	#else
		#include "WProgram.h"
	#endif

	class RTCDS1307 {  
	  public:
		RTCDS1307();
		void setClock();//update clock registers from local store
		void readClock();//read registers (incl sqw) to local store
		void start();//immediate; does not require setClock();
		void stop();//immediate; does not require setClock();
		
		int getSeconds();
		int getMinutes();
		int getHours();
		int getDate();		
		int getMonth();
		int getYear();

		//must also call setClock() after any of these
		//before next readClock(). Note that invalid dates are not
		//corrected by the clock. All the clock knows is when it should 
		//roll over to the next month rather than the next date in the same month.
		void setSeconds(int);
		void setMinutes(int);
		void setHours(int); //setHours rejects values out of range for the current 12/24 mode
		void setDate(int);//allows 1-31 for *all* months.
		void setMonth(int);
		void setYear(int);
		
	private:
		byte _reg0_sec;
		byte _reg1_min;
		byte _reg2_hour;
		byte _reg3_day;
		byte _reg4_date;
		byte _reg5_month;
		byte _reg6_year;
		byte _reg7_sqw;
		
		void writeData(byte, byte);//write a single value to a register
		byte decToBcd(byte);
		byte bcdToDec(byte);
	};
#endif